package com.zoe.onelink.core.i18n;

import com.alibaba.fastjson.JSON;
import com.zoe.onelink.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.text.MessageFormat;
import java.util.Locale;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-03-08
 */
@Slf4j
@RequiredArgsConstructor
public class OnelinkResourceBundleMessageSource extends ResourceBundleMessageSource {

    private final I18nMessageProvider i18nMessageProvider;

    @Override
    protected String getMessageInternal(String code, Object[] args, Locale locale) {
        if (this.i18nMessageProvider == null) {
            return super.getMessageInternal(code, args, locale);
        }
        String message;
        try {
            String messageTemplate = this.i18nMessageProvider.getMessageTemplate(locale, code);
            message = StrUtil.isBlank(messageTemplate) ? super.getMessageInternal(code, args, locale) : MessageFormat.format(messageTemplate, args);
        } catch (Exception e) {
            log.error("国际化失败,code:{},args:{},locale:{},exMsg:{}", code, JSON.toJSONString(args), locale, e.getMessage());
            message = code;
        }
        return message;
    }


}
